#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define NUM_OF_TASKS 5

void *downloadfile(void *filename) {
  printf("I am downloading the file %s!\n", (char *) filename);
  sleep(rand() % 10);
  long downloadtime = rand() % 100;
  printf("I finish downloading the file within %d minutes!\n", downloadtime);
  pthread_exit((void *) downloadtime);
}

int main(int argc, char *argv[]) {
  char files[NUM_OF_TASKS][20] = {"file1.avi", "file2.rmvb", "file3.mp4", "file4.wmv", "file5.flv"};
  pthread_t threads[NUM_OF_TASKS];
  int rc;
  int t;
  int downloadtime;

  pthread_attr_t thread_attr;
  pthread_attr_init(&thread_attr);
  pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);

  for (t = 0; t < NUM_OF_TASKS; t++) {
    printf("creating thread %d, please help me to download %s\n", t, files[t]);
    rc = pthread_create(&threads[t], &thread_attr, downloadfile, (void *) files[t]);
    if (rc) {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }

  pthread_attr_destroy(&thread_attr);

  for (t = 0; t < NUM_OF_TASKS; t++) {
    pthread_join(threads[t], (void **) &downloadtime);
    printf("Thread %d downloads the file %s in %d minutes.\n", t, files[t], downloadtime);
  }

  pthread_exit(NULL);
}
